<?php

/**
 * blacklist actions.
 *
 * @package    sf_sandbox
 * @subpackage blacklist
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class blacklistActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeShow(sfWebRequest $request)
  {
  	   $this->setDecorator();
  	   ////Проверка на регистрацию
    	if(!$this->getUser()->isAuthenticated())
		  	{
		  	 $this->getUser ()->setFlash ( 'error', 'Только для зарегистрированных пользователей' );
		  	$this->redirect('@registration');
		  	}
  	   ///
  	   
  	    if(!$request->getParameter('page'))
  	    {
  	    	$this->page = 1;
  	    }
  	    else 
  	    {
  	    	$this->page=$request->getParameter('page');
  	    }
  	
        $c = new Criteria ( );		
		$c->add( BlacklistPeer::USER_ID, $this->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ) );
		$c->addDescendingOrderByColumn ( BlacklistPeer::ID );
		
		$pager = new sfPropelPager('Blacklist', sfConfig::get('app_blacklist_perpage'));
        $pager->setCriteria($c);
        $pager->setPeerMethod('doSelect');        
        $pager->setPage($this->page);
        $pager->init();
        $this->black = $pager;
        
       ///Кидаем пользователя в layout
	     $this->getRequest()->setAttribute('v', $this->getUser()->getProfile());
		////
  }

  public function executeAdd(sfWebRequest $request)
  {
  	  	
  	   ////Проверка на регистрацию
    	if(!$this->getUser()->isAuthenticated())
		  	{
		  	 $this->getUser ()->setFlash ( 'error', 'Только для зарегистрированных пользователей' );
		  	$this->redirect('@registration');
		  	}
  	   ///
  	
    $user = sfGuardUserPeer::retrieveByPK($this->getRequestParameter('id'));
    $this->forward404Unless($user ); 
    
       /// проверяем нет ли в фаворитах, если нет добавляем
		 $cf = new Criteria();
		 $cf->add(FavoritePeer::USER_ID,$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
		 $cf->add(FavoritePeer::FAV_ID,$user->getId());
		 $cnt = FavoritePeer::doCount($cf);
		 if($cnt==0)
		 {
		 	$f = new Favorite();
		 	$f->setUserId($this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
		 	$f->setFavId($user->getId());
		 	$f->save();
		 }
		///
    
    
    $c = new Criteria();
    $c->add(BlacklistPeer::USER_ID,$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
    $c->add(BlacklistPeer::BLACK_ID,$this->getRequestParameter('id'));
    $cnt = BlacklistPeer::doCount($c);
    if($cnt==0)
    {
    $b = new Blacklist();
    $b->setUserId($this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
    $b->setBlackId($this->getRequestParameter('id'));
    $b->save();
    
    }
    $this->redirect('@black_list');
  }
  
  public function executeRemove(sfWebRequest $request)
  {
  	  	
  	   ////Проверка на регистрацию
    	if(!$this->getUser()->isAuthenticated())
		  	{
		  	 $this->getUser ()->setFlash ( 'error', 'Только для зарегистрированных пользователей' );
		  	$this->redirect('@registration');
		  	}
  	   ///
    $bl = BlacklistPeer::retrieveByPK($this->getRequestParameter('id'));
    $this->forward404Unless($bl ); 
      if($bl->getUserId()==$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ))
    {
     $bl->delete();
    }
    $this->redirect('@black_list');
  }
  
  
	public function setDecorator()
	{
		
		$module = $this->getRequest()->getParameter('module');
		$action = $this->getRequest()->getParameter('action');
		
		$path_layout = sfConfig::get('sf_app_template_dir').'/layouts/'.sfConfig::get('app_name').'/'.$module.'.php';
		$path_template = sfConfig::get('sf_app_module_dir').'/'.$module.'/templates/'.sfConfig::get('app_name').'/'.$action.'Success.php';
		if(file_exists($path_layout))
		{ $l = 'layouts/'.sfConfig::get('app_name').'/'.$module; }
		else
		{ $l = sfConfig::get('app_name').'_layout'; }
		if(file_exists($path_template))
		{ $t = sfConfig::get('app_name').'/'.$action; }
		else
		{ $t = $action; }
			
	/*
     * Установка layout
     */	
			$this->setLayout ( $l );
		
     /*
     *  Устанавливаем шаблон
     */
		
			$this->setTemplate ( $t );
	}
  
}
